package com.zhixun.zhlq.utils;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

public class DateUtil extends DateUtils {
	public static String DATE_FORMAT = "yyyy-MM-dd";
	public static String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	public static String DATE_FORMAT_CHINESE = "yyyy年M月d日";
	
	private static String[] parsePatterns = { 
    "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", 
    "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", 
    "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM" };

	public static String getDate(){
		return getDate(DateUtil.DATE_FORMAT);
	}
	
	public static String getDate(int index){
		return getDate(parsePatterns[index]);
	}

	public static String getDate(String pattern){
		return DateFormatUtils.format(new Date(), pattern);
	}
	
	public static String formatDate(Date date){
		return formatDate(date, "");
	}
	
	public static String formatDate(Date date, String pattern){
		if (StringUtil.isEmpty(date)) {
			date = new Date();
		} else if (StringUtil.isEmpty(pattern)) {
			pattern = DateUtil.DATE_FORMAT;
		}
		return DateFormatUtils.format(date, pattern);
	}

	public static String formatDateTime(Date date){
		return formatDate(date, DateUtil.DATE_TIME_FORMAT);
	}

	public static String getTime(){
		return formatDate(new Date(), "HH:mm:ss");
	}

	public static String getDateTime(){
	    return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
	}

	public static String getYear(){
		return formatDate(new Date(), "yyyy");
	}

	public static String getMonth(){
		return formatDate(new Date(), "MM");
	}

	public static String getDay(){
		return formatDate(new Date(), "dd");
	}

	public static String getWeek(){
		return formatDate(new Date(), "E");
	}
	//将日期字符串转换成Date格式
	public static Date parseDate(Object str){
	    if (str == null)
	      return null;
	    try
	    {
	      return parseDate(str.toString(), parsePatterns); } catch (ParseException e) {
	    }
	    return null;
	}
	/*public static Date parseDate(Object str, int parsePatternsIndex){
	    if (str == null)
	      return null;
	    try
	    {
	      return parseDate(str.toString(), parsePatterns[parsePatternsIndex]); } catch (ParseException e) {
	    }
	    return null;
	}*/

	/**
	 * 与当前日期相差几天
	 * @param date
	 * @return
	 */
	public static long pastDays(Date date){
	    long t = new Date().getTime() - date.getTime();
	    return t / 86400000L;
	}
	/**
	 * 与当前日期相差几小时
	 * @param date
	 * @return
	 */
	public static long pastHour(Date date){
	    long t = new Date().getTime() - date.getTime();
	    return t / 3600000L;
	}
	/**
	 * 与当前日期相差几分钟
	 * @param date
	 * @return
	 */
	public static long pastMinutes(Date date){
	    long t = new Date().getTime() - date.getTime();
	    return t / 60000L;
	}

	public static String formatDateTime(long timeMillis){
	    long day = timeMillis / 86400000L;
	    long hour = timeMillis / 3600000L - day * 24L;
	    long min = timeMillis / 60000L - day * 24L * 60L - hour * 60L;
	    long s = timeMillis / 1000L - day * 24L * 60L * 60L - hour * 60L * 60L - min * 60L;
	    long sss = timeMillis - day * 24L * 60L * 60L * 1000L - hour * 60L * 60L * 1000L - min * 60L * 1000L - s * 1000L;
	    return (day > 0L ? day + "," : "") + hour + ":" + min + ":" + s + "." + sss;
	}

	/**
	 * 获取两个日期之间的天数
	 * @param before
	 * @param after
	 * @return
	 */
	public static double getDistanceOfTwoDate(Date before, Date after){
	    long beforeTime = before.getTime();
	    long afterTime = after.getTime();
	    return (afterTime - beforeTime) / 86400000L;
	}

	/**
	 * 获取两个时间之间的分钟数
	 * @param before
	 * @param after
	 * @return
	 */
	public static double getMinuteOfTwoDate(Date before, Date after) {
		long beforeTime = before.getTime();
		long afterTime = after.getTime();
		return (afterTime - beforeTime) / 1000/60;
	}
	public static String addYear(Date date, int i, String pattern){
		return formatDate(i,3,date, pattern);
	}
	public static String addMonth(Date date, int i, String pattern){
		return formatDate(i,2,date, pattern);
	}
	public static String addDay(Date date, int i, String pattern){
		return formatDate(i,1,date, pattern);
	}
	/**
	 * 在当前日期上+number(年、月、日)
	 * @param number	日期增加number(年、月、日)
	 * @param type		控制类型(年type=3、月type=2、日type=1)
	 * @return
	 */
	private static String formatDate(int number, int type, Date date, String pattern){
		Calendar cal = Calendar.getInstance();
		if (date == null) {
			date = new Date();
		}
		cal.setTime(date);
		if (1 == type) {//操作日期(日)
			cal.add(Calendar.DATE, number);
		} else if (2 == type) {//操作日期(月)
			cal.add(Calendar.MONTH, number);
		} else if (3 == type) {//操作日期(年)
			cal.add(Calendar.YEAR, number);
		}
		return formatDate(cal.getTime(), pattern);
	}

	public static String getQuarter (String month) {
		String quarter = "";
		int m = Integer.parseInt(month);
		if (m >= 1 && m == 3) {
			quarter = "1";
		}
		if (m >= 4 && m <= 6) {
		quarter = "2";
		}
		if (m >= 7 && m <= 9) {
		quarter = "3";
		}
		if (m >= 10 && m <= 12) {
		quarter = "4";
		}
		return quarter;
	}
}